﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.Core;
using SmartSpace.Core.Metadata;
using SmartSpace.Data;
using SsTool.Common;
using SsTool.Metadata.Windows;

namespace SsTool.Metadata
{
    /// <summary>
    /// Interaction logic for EntityMetadataPage.xaml
    /// </summary>
    public partial class EntityMetadataPage : Page
    {
        public EntityMetadataPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(EntityMetadataPage_Loaded);
        }

        void EntityMetadataPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDatas();
        }

        private void LoadDatas()
        {
            IDataService service = App.Get<IDataService>();
            _ctlGrid.ItemsSource = service.FindEntities(0, 999);
        } 

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            SsTool.Metadata.Windows.CreateEntityMetadataWindow window = new Windows.CreateEntityMetadataWindow();
            if (window.ShowDialog() == true)
            {
                LoadDatas();
            }
        }

        private void _ctlBtnToTable_Click(object sender, RoutedEventArgs e)
        {
            EntityMetadata selectedEntity = _ctlGrid.SelectedItem as EntityMetadata;
            if (selectedEntity != null)
            {
                IDataService service = App.Get<IDataService>();
                if (service.ToTable(selectedEntity) == true)
                {
                    MessageBox.Show("创建成功!");
                }
                else
                {
                    MessageBox.Show("创建失败!");
                }
            }
        }

        private void _ctlBtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            EntityMetadata selectedEntity = _ctlGrid.SelectedItem as EntityMetadata;
            if (selectedEntity != null)
            {
                IDataService service = App.Get<IDataService>();
                selectedEntity = service.FindEntity(selectedEntity.Id);
                SsTool.Metadata.Windows.CreateEntityMetadataWindow window = new Windows.CreateEntityMetadataWindow(selectedEntity);
                if (window.ShowDialog() == true)
                {
                    LoadDatas();
                }
            }
        }

        private void _ctlGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EntityMetadata selectedEntity = _ctlGrid.SelectedItem as EntityMetadata;
            if (selectedEntity != null)
            {
                IDataService service = App.Get<IDataService>();
                EntityMetadata entity = service.FindEntity(selectedEntity.Id);
                List<EntityMetadata> relations = service.FindRelationEntities(entity.Id);
                EntityMetadata primary = service.FindPrimaryEntity(entity.Id);
                _ctlGridProperty.ItemsSource = entity.Properties;
                _ctlGridRelation.ItemsSource = relations;
                if (primary != null)
                {
                    _ctlGridPrimary.ItemsSource = new EntityMetadata[] { primary };
                }
                else
                {
                    _ctlGridPrimary.ItemsSource = null;
                }
            }
        }

        #region 实体关联关系操作
        private void _ctlAddRelation_Click(object sender, RoutedEventArgs e)
        {
            EntityMetadata selectedEntity = _ctlGrid.SelectedItem as EntityMetadata;
            if (selectedEntity != null)
            {
                SelectEntityMetadataWindow window = new SelectEntityMetadataWindow();
                if (window.ShowDialog() == true)
                {
                    if (window.SelectedEntity.Id == selectedEntity.Id)
                    {
                        MessageBox.Show("添加实体关联关系失败，不能关联自己!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    IDataService dataService = App.Get<IDataService>();
                    if (dataService.SaveRelation(selectedEntity, window.SelectedEntity, false))
                    {
                        List<EntityMetadata> relations = dataService.FindRelationEntities(selectedEntity.Id);
                        this._ctlGridRelation.ItemsSource = relations;
                    }
                    else
                    {
                        MessageBox.Show("添加实体关联关系失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }

        private void _ctlDeleteRelation_Click(object sender, RoutedEventArgs e)
        {

        }
        #endregion

        #region 属性操作
        private void _ctlAddProperty_Click(object sender, RoutedEventArgs e)
        {
            EntityMetadata selectedEntity = _ctlGrid.SelectedItem as EntityMetadata;
            if (selectedEntity != null)
            {
                IDataService service = App.Get<IDataService>();
                EntityMetadata entity = service.FindEntity(selectedEntity.Id);
                CreatePropertyWindow window = new CreatePropertyWindow(entity);
                if (window.ShowDialog() == true)
                {
                    _ctlGridProperty.ItemsSource = null;
                    _ctlGridProperty.ItemsSource = entity.Properties;
                }
            }
        }

        private void _ctlEditProperty_Click(object sender, RoutedEventArgs e)
        {
            EntityMetadata selectedEntity = _ctlGrid.SelectedItem as EntityMetadata;
            if (selectedEntity != null)
            {
                SmartSpace.Data.PropertyMetadata property = _ctlGridProperty.SelectedItem as SmartSpace.Data.PropertyMetadata;
                if (property != null)
                {
                    IDataService service = App.Get<IDataService>();
                    EntityMetadata entity = service.FindEntity(selectedEntity.Id);
                    CreatePropertyWindow window = new CreatePropertyWindow(entity,  property);
                    if (window.ShowDialog() == true)
                    {
                        _ctlGridProperty.ItemsSource = null;
                        _ctlGridProperty.ItemsSource = entity.Properties;
                    }
                }
            }
        }

        private void _ctlDeleteProperty_Click(object sender, RoutedEventArgs e)
        {

        }
        #endregion
    }
}
